/*  Copyright 2011 Jason Jones
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.jasonwjones.hyperion.jessub.formattingvalue.date;

import org.joda.time.DateTime;

import com.jasonwjones.hyperion.jessub.formattingvalue.AbstractFormattingValue;
import com.jasonwjones.hyperion.jessub.formattingvalue.FormattingValue;

/**
 * The DateFormattingValue class. Represents a paramter in the Jessub micro-
 * language that is date-related. This is the real heart of the operatoin
 * @author jasonwjones
 *
 */
public class DateFormattingValue extends AbstractFormattingValue implements
		FormattingValue {

	protected DateTime basisDate;
	protected DateAdjustmentSet dateAdjustmentSet;
	
	/**
	 * Constructor is private so we are forced to go through one of the static
	 * initializers.
	 * @param basisDate the date that will be used for formatting
	 * @param formattingCode the String contents that will get parsed for
	 * adjustment values
	 */
	private DateFormattingValue(DateTime basisDate, String formattingCode) {
		this.basisDate = basisDate;
		this.dateAdjustmentSet = new DateAdjustmentSet(formattingCode.split(","));
	}
		
	/**
	 * Creates a new DateFormattingValue object based on the time that this
	 * routine is called
	 * @param code the String contents that will be parsed for adjustment values
	 * @return a new DateFormattingValue object
	 */
	public static DateFormattingValue createWithNow(String code) {
		return new DateFormattingValue(DateTime.now(), code);		
	}
	
	/**
	 * Method for FormattingValue compliance. Creates a native Java Date object
	 * by applying the date adjustment values to the basis date.
	 * @return the resulting Date object
	 */
	public Object createValue() {
		return dateAdjustmentSet.applyTo(basisDate).toDate();
	}
	
}
